package com.wm.exam.controller;

import com.wm.exam.common.Const;
import com.wm.exam.common.ServerResponse;
import com.wm.exam.pojo.Category;
import com.wm.exam.pojo.ExamHistory;
import com.wm.exam.pojo.User;
import com.wm.exam.service.*;
import com.wm.exam.vo.ExamVo;
import com.wm.exam.vo.PaperVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ExamController {
    @Autowired
    private PaperVoService paperVoService;
    @Autowired
    private ExamVoService examVoService;
    @Autowired
    private ExamHistoryService examHistoryService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private PaperService paperService;



    /**
     * 跳转到试卷页面
     */
    @RequestMapping("exam/do")
    public String doExam(HttpServletRequest request, Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        // 查询出结果集合
        List<ExamVo> list = examVoService.getByUserClazz(user.getClazz());
        model.addAttribute("list", list);
        // 计算每道题分数和答题总时间
        String timeValue = list.get(0).getTimeValue();
        int scope = (int) (list.get(0).getScopeValue() / list.size());
        Map map = new HashMap<>();
        // 放置时间
        map.put("time", timeValue);
        // 放置每道题的分数值
        map.put("scope", scope);
        // 放置总题数
        map.put("size", list.size());
        // 放置总分数
        map.put("totalScope", list.get(0).getScopeValue());
        model.addAttribute("map", map);
        return "exam";
    }

    /**
     * 用户提交试卷得出分数
     */
    @RequestMapping("exam/returnScope")
    public String returnScope(@RequestParam("answer") String answer,
                                      HttpServletRequest request, Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        List<ExamVo> list = examVoService.getByUserClazz(user.getClazz());
        String categoryId = list.get(0).getUuid();
        double scope = examVoService.getScope(list,answer);
        model.addAttribute("scope", scope);
        // 写入数据库记录一下
        int result  = examHistoryService.addHistory(user,answer, categoryId, scope);
        if (result > 0) {
            // 显示成绩的界面
            return "examResult";
        }
        // 返回 404页面
        return "404";
    }
    /**
     * 检查用户是否考试过相同的试卷
     */
    @RequestMapping("exam/checkExam")
    @ResponseBody
    public ServerResponse checkExam(HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorMessage("请先登录");
        }
        // 先查询用户的班级
        Category category = categoryService.getByClazz(user.getClazz());
        if (null == category) {
            return ServerResponse.createByErrorMessage("未查询到相关考试试题，请联系管理员");
        }
        ExamHistory history = examHistoryService.getByUUIDAndStuNum(user.getStuNum(), category.getUuid());
        if (null == history) {
            return ServerResponse.createBySuccessMessage("满足考试要求");
        }
        return ServerResponse.createByErrorMessage("您已经参加过考试，请在左侧查看考试纪录！");
    }

    /**
     * 跳转到确认考试界面
     */
    @RequestMapping("exam/confirmExam")
    public String confirmExam(HttpServletRequest request, Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        PaperVo paperVo = paperVoService.getByMajor(user.getMajor());
        // 查询试题总数
        int total = paperService.getByUUID(paperVo.getUuid());
        model.addAttribute("total", total);
        model.addAttribute("paperVo", paperVo);
        return "confirmExam";
    }
}
